package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtArticleDao;
import com.intellagric.mapper.AgtArticleMapper;
import com.intellagric.mapper.AgtResourceMapper;
import com.intellagric.module.agritech.AgtArticleVo;
import com.intellagric.pojo.AgtArticle;
import com.intellagric.pojo.AgtArticleExample;
import com.intellagric.pojo.AgtResource;
import com.intellagric.service.agritech.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/23 13:07
 * @Description:
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private AgtArticleMapper agtArticleMapper;

    @Autowired
    private AgtArticleDao agtArticleDao;

    @Autowired
    private AgtResourceMapper agtResourceMapper;

    @Override
    public ResponseMessage addArticle(AgtArticle article) {
        AgtResource agtResource = new AgtResource();
        Date createdTime = new Date();
        String resourceId = UUIDUtils.getID();
        String articleId = UUIDUtils.getID();

        agtResource.setId(resourceId);
        agtResource.setFid(articleId);
        agtResource.setResourceTypeId(article.getResourceTypeId());
        System.out.println("--------:  "+article.getResourceTypeId());
        agtResource.setCreatedTime(createdTime);

        article.setId(articleId);
        article.setResourceId(resourceId);
        article.setCreatedTime(createdTime);

        return agtResourceMapper.insertSelective(agtResource) == 1 ? (
                agtArticleMapper.insertSelective(article) == 1 ? ResponseMessage.success().add("add", article) : ResponseMessage.fail()
        ) : ResponseMessage.fail();
//        return agtArticleMapper.insertSelective(article) == 1 ? ResponseMessage.success().add("add", article) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteArticle(String id) {
        return agtArticleMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editArticle(AgtArticle article) {
        return agtArticleMapper.updateByPrimaryKeySelective(article) == 1 ? ResponseMessage.success().add("update", article) : ResponseMessage.fail();
    }

    @Override
    public AgtArticle getArticleById(String id) {
        return agtArticleMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getArticleList() {
        AgtArticleExample agtArticleExample = new AgtArticleExample();
        agtArticleExample.setOrderByClause("created_time ASC");
        agtArticleExample.createCriteria().andIdIsNotNull();
        List<AgtArticle> agtArticleList = agtArticleMapper.selectByExample(agtArticleExample);
        return LayuiDataGridResult.success().add(agtArticleList, agtArticleList.size());
    }

    @Override
    public LayuiDataGridResult getArticleByPage(int page, int limit) {
        PageHelper.startPage(page, limit, "created_time ASC");
        AgtArticleExample articleExample = new AgtArticleExample();
        articleExample.createCriteria().andIdIsNotNull();
        List<AgtArticle> articleList = agtArticleMapper.selectByExample(articleExample);
        return LayuiDataGridResult.success().add(articleList, this.getArticleList().getCount());
    }

    @Override
    public LayuiDataGridResult getArticleListByVo(AgtArticleVo agtArticleVo, int page, int limit) {
        PageHelper.startPage(page, limit, "created_time DESC");
        List<AgtArticle> articleList = agtArticleDao.selectArticleListByVo(agtArticleVo);
        return LayuiDataGridResult.success().add(articleList, this.getArticleList().getCount());
//        return LayuiDataGridResult.success().add(articleList, articleList.size());
    }
}
